<template>
  <div
    style="
      width: 1015px;
      height: 105px;
      margin-left: 200px;
      top: 25px;
      background-color: rgb(255, 255, 255);
      position: absolute;
    "
  >
    <el-form ref="queryFormRef" :model="QueryData.form">
      <el-form-item prop="text" class="inputSql">
        <el-input
          v-model="QueryData.form.text"
          type="search"
          placeholder="按问题查询"
        />
      </el-form-item>
      <el-form-item prop="dateSql" class="inputSql">
        <el-input
          v-model="QueryData.form.dateSql"
          type="date"
          placeholder="按日期查询"
        />
      </el-form-item>
      <el-form-item prop="createBy" class="inputSql">
        <el-input
          v-model="QueryData.form.createBy"
          type="search"
          placeholder="按提问者查询"
        />
      </el-form-item>
      <el-button type="primary" class="querybt1" @click="reDraw(queryFormRef)"
        >查询☚</el-button
      >
      <el-button type="primary" class="querybt1" @click="reload()"
        >刷新☚</el-button
      >
    </el-form>

    <div class="container">
      <ul class="tableheader">
        <li style="color: Navy">问题</li>
        <li style="color: Navy">提问者</li>
        <li style="color: Navy">提问时间</li>
      </ul>
      <br />

      <hr />
      <ul v-if="dataRef.length" class="tablebody">
        <li v-for:="(item, key) in dataRef">
          <div class="row" style="padding-left: 50px">
            {{ item.text }}
          </div>
          <div class="row" style="padding-left: 50px">
            {{ item.createBy }}
          </div>
          <div class="row">{{ item.createTime }}</div>
          <!-- <el-button
            type="primary"
            class="querybt"
            @click="drawUpdateBox(item.id)"
            >修改</el-button
          > -->
          <el-button
            type="primary"
            class="querybt"
            @click="drawAnswerBox(item.id)"
            >回复</el-button
          >
        </li>
      </ul>

      <p v-else>
        <img
          src="https://img-qn-0.51miz.com/preview/element/00/01/13/33/E-1133330-94144800.jpg!/quality/90/unsharp/true/compress/true/format/jpg/fh/260"
        />
      </p>
    </div>
    <div class="addbox">
      <hr />
      <h2
        style="
          color: Navy;
          font-size: 20px;
          font-weight: 900px;
          position: absolute;
          top: 0;
        "
      >
        发表留言
      </h2>
      <el-form ref="insertFormRef" :model="insertData.form">
        <el-form-item prop="text">
          <el-input
            v-model="insertData.form.text"
            type="textarea"
            class="insertinput"
            placeholder="请发表友好的言论哦~"
          />
        </el-form-item>
        <el-button
          type="primary"
          class="insertbt"
          @click="insertReq(insertFormRef)"
          >发布</el-button
        >
      </el-form>
    </div>
    <!-- 回复框 -->
    <div class="AnswerBox">
      <h2
        style="
          color: Navy;
          font-size: 20px;
          font-weight: 900px;
          position: absolute;
          top: 0;
        "
      >
        回复问题
      </h2>
      <el-form ref="updateFormRef" :model="updateData.form">
        <el-form-item prop="text">
          <el-input
            v-model="updateData.form.text"
            type="textarea"
            class="updateinput"
            placeholder="请发表友好的言论哦~"
          />
        </el-form-item>
        <el-button
          type="primary"
          class="updatebt"
          @click="answerReq(updateFormRef)"
          >发送</el-button
        >
        <el-button
          type="primary"
          class="updatebt"
          style="left: 650px"
          @click="drawAnswerBox()"
          >取消</el-button
        >
      </el-form>
    </div>
    <!-- 修改框 -->
    <div class="updatebox">
      <h2
        style="
          color: Navy;
          font-size: 20px;
          font-weight: 900px;
          position: absolute;
          top: 0;
        "
      >
        修改留言
      </h2>
      <el-form ref="updateFormRef" :model="updateData.form">
        <el-form-item prop="text">
          <el-input
            v-model="updateData.form.text"
            type="textarea"
            class="updateinput"
            placeholder="留下你想修改的内容吧~"
          />
        </el-form-item>
        <el-button
          type="primary"
          class="updatebt"
          @click="updateReq(updateFormRef)"
          >确认修改</el-button
        >
        <el-button
          type="primary"
          class="updatebt"
          style="left: 650px"
          @click="drawUpdateBox()"
          >取消</el-button
        >
      </el-form>
    </div>
  </div>
</template>

<script setup>
import { reactive, ref } from "@vue/reactivity";
import { querymessageBoards } from "../../apis/query/querymessage.js";
import { delete_msg } from "../../apis/delete/deleteMessage.js";
import { insert_msg } from "../../apis/inset/insertMessage.js";
import { update_msg } from "../../apis/update/updateMessage";
const queryFormRef = ref();
let dataRef = reactive([]);
//查询数据
const QueryData = reactive({
  form: {
    text: "",
    createBy: "",
  },
  rules: {},
});
const queryReq = async (queryFormRef) => {
  await queryFormRef.validate((valid) => {
    //校验表单是否通过
    if (valid) {
      querymessageBoards(QueryData.form).then((response) => {
        var x;
        for (x in response.data) {
          dataRef[x] = response.data[x];
          console.log(dataRef[x]);
        }
      });
    }
  });
};
const reDraw = (queryFormRef) => {
  queryReq(queryFormRef);
};
//删除数据
const deletedata = reactive({ form: { id: 0 }, rules: {} });
const deleteReq = async (id) => {
  deletedata.form.id = id;
  delete_msg(deletedata.form).then((res) => {
    console.log(res?.message);
  });
};
//插入数据
const insertFormRef = ref();
const insertData = reactive({
  form: {
    text: "",
    publisher: "",
  },
});
const insertReq = async (insertFormRef) => {
  await insertFormRef.validate((valid) => {
    //校验表单是否通过
    if (valid) {
      insert_msg(insertData.form).then((response) => {
        console.log(response?.message);
      });
    }
  });
};
//更新数据
const updateFormRef = ref();
const updateData = reactive({
  form: {
    id: 0,
    text: "",
  },
  rules: {},
});
const updateReq = async (updateFormRef) => {
  await updateFormRef.validate((valid) => {
    //校验表单是否通过
    if (valid) {
      update_msg(updateData.form).then((response) => {
        console.log(response?.message);
      });
    }
  });
};
let updateBoxFlag = true;
const drawUpdateBox = (id) => {
  if (updateBoxFlag) {
    $(".updatebox").css("z-index", "999");
    updateData.form.id = id;
  } else {
    $(".updatebox").css("z-index", "-1");
  }
  updateBoxFlag = !updateBoxFlag;
};

let answerBoxFlag = false;
const drawAnswerBox = (id) => {
  if (answerBoxFlag) {
    $(".AnswerBox").css("display", "none");
  } else {
    $(".AnswerBox").css("display", "block");
  }
  answerBoxFlag = !answerBoxFlag;
};
//更新按钮

const reload = () => {
  console.log("刷新！");
  location.reload();
};
</script>

<style scoped>
.inputSql {
  width: 150px;
  padding: 50px 30px;
  float: left;
}
.querybt {
  margin: 24px;
  float: left;
}
.querybt1 {
  margin: 50px;
  float: left;
}
.container {
  position: absolute;
  width: 1015px;
  height: 455px;
  margin-top: 105px;
  background-color: white;
}
.tableheader li {
  width: 250px;
  padding: 0px 15px;
  float: left;
  font-size: 25px;
  font-weight: 900;
  text-align: left;
}
.tableheader {
  list-style-type: none;
}

.tablebody {
  width: calc(100%);
  max-height: 400px;
  list-style-type: none;
  float: left;
  text-align: left;
  overflow-y: scroll;
  overflow-x: hidden;
  position: absolute;
  top: 38px;
  left: -40px;
}
li {
  display: flex;
  width: 1015px;
}
li div {
  width: 305px;
  height: 30px;
}
.tablebody li:hover {
  background-color: rgb(230, 245, 245);
}
.addbox {
  position: absolute;
  top: 550px;
  height: 150px;
  width: 1015px;
  background-color: white;
  float: left;
}
.updatebox {
  position: absolute;
  top: 100px;
  height: 450px;
  width: 1015px;
  background-color: white;
  float: left;
  z-index: -1;
}
.insertinput {
  position: absolute;
  width: 350px;
  top: 30px;
  left: 100px;
}
.updateinput {
  position: absolute;
  width: 700px;
  max-height: 230px;
  overflow: hidden;
  top: 30px;
  left: 100px;
}
.insertbt {
  position: absolute;
  width: 100px;
  height: 40px;
  left: 800px;
  top: 50px;
}
.updatebt {
  position: absolute;
  width: 100px;
  height: 40px;
  left: 150px;
  top: 300px;
}
.row {
  overflow: hidden;
  height: 50px;
}
.AnswerBox {
  position: absolute;
  top: 100px;
  height: 450px;
  width: 1015px;
  background-color: white;
  float: left;
  display: none;
}
</style>
